package com.yuncheng.spcyApi.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import org.apache.commons.collections.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuncheng.spcyApi.bo.JcyExistLbBo;
import com.yuncheng.spcyApi.constant.common.LogConstant;
import com.yuncheng.spcyApi.constant.common.SpcyCommonConstant;
import com.yuncheng.spcyApi.constant.common.SpcyConstant;
import com.yuncheng.constant.SysUserConstant;
import com.yuncheng.spcyApi.entity.SpcyRsHcy;
import com.yuncheng.entity.YcSysUser;
import com.yuncheng.entity.YcSysUserDepart;
import com.yuncheng.entity.YcSysUserRole;
import com.yuncheng.spcyApi.mapper.SpcyRsHcyMapper;
import com.yuncheng.spcyApi.mapper.YcSysUserMapper;
import com.yuncheng.spcyApi.service.ISpcyLogService;
import com.yuncheng.spcyApi.service.ISpcyRsHcyService;
import com.yuncheng.spcyApi.utils.GetCurrentUserUtils;
import com.yuncheng.spcyApi.utils.GetUuIdUtils;

import com.yuncheng.vo.CurrentUserVo;
import com.yuncheng.spcyApi.vo.jcy.HcyJbxxVo;
import com.yuncheng.spcyApi.vo.jcy.RsHcyVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.yuncheng.common.base.service.impl.BaseServiceImpl;
import utils.DateConversionUtils;
import utils.LogUtils;
import utils.ObjectToMapUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description: 审评查验-核查员库
 * @Author: csx
 * @Date: 2023-03-03
 * @Version: V1.0
 */
@Service
public class SpcyRsHcyServiceImpl extends BaseServiceImpl<SpcyRsHcyMapper, SpcyRsHcy> implements ISpcyRsHcyService {
    @Resource
    private ISpcyLogService spcyLogService;

    @Resource
    private YcSysUserMapper ycSysUserMapper;

    @Resource
    private SpcyRsHcyMapper spcyRsHcyMapper;

    @Override
    public void impRsHcyData(Map map, String ywflCode, Integer xh) {
        //以下是新增系统用户需要用到的默认参数
        String userID = GetUuIdUtils.ReplaceUuId(); // 用户id
        String oldUserId = ""; // 旧用户id
        String workNO = GetUuIdUtils.ReplaceUuId(); // 工号

        YcSysUser sysUser = new YcSysUser();
        SpcyRsHcy rsHcy = new SpcyRsHcy();
        BeanUtil.copyProperties(map,sysUser); // 将导入数据复制替换
        BeanUtil.copyProperties(map,rsHcy); //  将导入数据复制替换

        String fuserType = ""; // 检查员、专家
        if (rsHcy != null && StringUtils.isNotEmpty(rsHcy.getFusertype())){
            fuserType = rsHcy.getFusertype();
        }else {
            return;
        }

        boolean isJcyType = false;
        boolean isAddZj = false;
        boolean isZjType = false;

        //本方法认为用户名跟账号相同，如果不是这种情况 请不要用此方法
        String USERNAME = "";
        String REALNAME = "";
        if (sysUser == null){
            return;
        }
        if (rsHcy == null){
            return;
        }
        // 去掉全角空格
        if (StringUtils.isNotBlank(sysUser.getRealname()) && StringUtils.isNotEmpty(sysUser.getRealname())) {
            USERNAME = sysUser.getRealname().replaceAll("\\s*", "");
            REALNAME = sysUser.getRealname().replaceAll("\\s*", "");

            USERNAME = USERNAME.replaceAll((char)12288+"","");
            REALNAME = REALNAME.replaceAll((char)12288+"","");
        }
        // 姓名为空
        if (StringUtils.isEmpty(USERNAME)){
            return;
        }

        String phone = "";
        String email = "";
        // 去掉全角空格
        if (StringUtils.isNotBlank(sysUser.getPhone()) && StringUtils.isNotEmpty(sysUser.getPhone())){
            phone = sysUser.getPhone().replaceAll("\\s*", "");
            phone = phone.replaceAll((char)12288+"","");
        }
        // 去掉全角空格
        if (StringUtils.isNotBlank(sysUser.getEmail()) && StringUtils.isNotEmpty(sysUser.getEmail())){
            email = sysUser.getEmail().replaceAll("\\s*", "");
            email = email.replaceAll((char)12288+"", "");
        }
        sysUser.setId(userID);
        sysUser.setUsername(USERNAME);
        sysUser.setRealname(REALNAME);
        sysUser.setPhone(phone);
        sysUser.setEmail(email);
        sysUser.setPassword(SysUserConstant.PASSWORD); // 默认密码
        sysUser.setSalt(SysUserConstant.SALT); // 加盐值
        sysUser.setIsRealUser(SysUserConstant.IS_REAL_USER); // 真实用户
        sysUser.setWorkNo(workNO); // 工号
        sysUser.setStatus(Integer.valueOf(SysUserConstant.STATUS)); // 有效状态
        sysUser.setDelFlag(Integer.valueOf(SysUserConstant.DEL_FLAG)); // 删除-有效状态

        // 判断检查员、核查员
        if (fuserType.equals(SpcyConstant.HCY_USERTYPE_JCY)) {
            sysUser.setOrgId(SpcyConstant.HCY_ORGID); // 部门编号
            sysUser.setOrgName(SpcyConstant.HCY_ORGNAME); // 部门名称
            sysUser.setOrgCode(SpcyConstant.HCY_ORGCODE); // 默认机构编码

            isJcyType = true;
        }else {
            sysUser.setOrgId(SpcyConstant.HCY_ZJ_ORGID); // 部门编号
            sysUser.setOrgName(SpcyConstant.HCY_ZJ_ORGNAME); // 部门名称
            sysUser.setOrgCode(SpcyConstant.HCY_ZJ_ORGCODE); // 默认机构编码

            isZjType = true;
        }
        sysUser.setUserOrder(xh);

        List<YcSysUser> sysUserList = new ArrayList<>();
        sysUserList = ycSysUserMapper.selectList(new LambdaQueryWrapper<YcSysUser>()
                .eq(YcSysUser::getRealname,REALNAME)
                .eq(YcSysUser::getDelFlag,0));

        // 不存在该用户的情况或者存在多条数据的时候，需要新增
        if(sysUserList.size() == 0 || sysUserList.size() > 1) {
            // 存在多条同账号和姓名，进行删除
            if (sysUserList.size()>1){

                for (YcSysUser user : sysUserList) {
                    user.setDelFlag(1);
                    ycSysUserMapper.updateById(user);

                    String operContent = "导入检察员，存在多条重复用户，删除系统用户：用户名["+user.getRealname()+"]; " +
                            "账号["+user.getUsername()+"]; 用户编号["+user.getId()+"];";
                    spcyLogService.addLogs(LogConstant.LOGS_SYS_USER, operContent, user.getId(),SpcyConstant.LOGS_SYS_TYPE_SC,SpcyConstant.LOGS_SYS_TYPE_SC);
                }
            }

            oldUserId = userID; // 赋值旧用户id
            sysUser.setCreateTime(new Date());
            ycSysUserMapper.insert(sysUser); // 新增用户

            isAddZj = true;

            // 新增日志
            String addSysUserOper = "新增用户：";
            Map<String, Object> addUserMap = ObjectToMapUtils.objectToMap(sysUser);
            addSysUserOper += LogUtils.MapToStringMethod(addUserMap);
            spcyLogService.addLogs(LogConstant.LOGS_SYS_USER,addSysUserOper,userID,LogConstant.LOGS_SYS_USER,SpcyConstant.LOGS_SYS_TYPE_XZ);

            // 新增部门用户关系
            YcSysUserDepart ycSysUserDepart = new YcSysUserDepart();
            ycSysUserDepart.setId(GetUuIdUtils.ReplaceUuId());
            ycSysUserDepart.setUserId(userID);
            // 判断核查员、专家
            if (fuserType.equals(SpcyConstant.HCY_USERTYPE_JCY)) {
                ycSysUserDepart.setDepId(SpcyConstant.HCY_ORGID); // 核查员部门编号
            }else {
                ycSysUserDepart.setDepId(SpcyConstant.HCY_ZJ_ORGID); // 专家部门编号
            }
            ycSysUserDepart.setPositionId(""); // 职务编号
            ycSysUserDepart.setIsMajorDep(SysUserConstant.IS_MAJOR_DEP); // 是否主部门
            ycSysUserMapper.addYcSysUserDept(ycSysUserDepart);

            String ycAddUserDepartOperCont = "新增系统用户部门联系：用户名["+sysUser.getRealname()+"]; 部门["+SpcyConstant.HCY_ORGNAME+"]; " +
                    "用户编号["+userID+"]; 部门编号["+SpcyConstant.HCY_ORGID+"];";
            spcyLogService.addLogs(LogConstant.LOGS_SYS_USER,ycAddUserDepartOperCont,userID,SpcyConstant.LOGS_SYS_TYPE_XZ,SpcyConstant.LOGS_SYS_TYPE_BC);

            // 核查员角色id
            String roleID = "";
            if (fuserType.equals(SpcyConstant.HCY_USERTYPE_JCY)) {
                roleID = SpcyConstant.getRoleIdByCode(ywflCode);
            }else {
                roleID = SpcyConstant.getZjRoleIdByCode(ywflCode);
            }


            // 新增角色用户关系
            YcSysUserRole ycSysUserRole = new YcSysUserRole();
            ycSysUserRole.setId(GetUuIdUtils.NotReplaceUuId());
            ycSysUserRole.setUserId(userID);
            ycSysUserRole.setRoleId(roleID);
            // 角色id不为空
            if (StringUtils.isNotEmpty(roleID)){
                ycSysUserMapper.addYcSysUserRole(ycSysUserRole);

                String ycAddUserRoleOperCont = "新增用户角色联系：用户名["+sysUser.getRealname()+"]; 用户编号["+userID+"]; " +
                        "角色编号["+roleID+"];]";
                spcyLogService.addLogs(LogConstant.LOGS_SYS_USER_ROLE,ycAddUserRoleOperCont,userID,LogConstant.LOGS_SYS_USER_ROLE,SpcyConstant.LOGS_SYS_TYPE_XZ);
            }

        }else {
            oldUserId = sysUserList.get(0).getId(); // 赋值旧用户id

            // 存在一条 不处理
            // 用户逻辑删除状态 不为正常 改为正常
            YcSysUser updateUser = sysUserList.get(0);
            if (StringUtils.isNotBlank(email)) {
                updateUser.setEmail(email);
            }
            if (StringUtils.isNotBlank(phone)){
                updateUser.setPhone(phone);
            }
            if (StringUtils.isNotBlank(sysUser.getFsfzh())){
                updateUser.setFsfzh(sysUser.getFsfzh());
            }
            updateUser.setSex(sysUser.getSex());
            updateUser.setFdwdh(sysUser.getFdwdh());
            updateUser.setFdwyx(sysUser.getFdwyx());
            updateUser.setBirthday(sysUser.getBirthday());
            updateUser.setFmz(sysUser.getFmz());
            updateUser.setFzzmm(sysUser.getFzzmm());
            updateUser.setFbyyx(sysUser.getFbyyx());
            updateUser.setFxl(sysUser.getFxl());
            updateUser.setFsxzy(sysUser.getFsxzy());
            updateUser.setFgzdw(sysUser.getFgzdw());
            updateUser.setFgzdwdz(sysUser.getFgzdwdz());
            updateUser.setFzw(sysUser.getFzw());
            updateUser.setFzc(sysUser.getFzc());
            updateUser.setFcsgz(sysUser.getFcsgz());
            updateUser.setFssqy(sysUser.getFssqy());
            ycSysUserMapper.updateById(updateUser);

            // 业务分类名称-获取角色id
            String roleID = "";

            // 判断检查员、核查员
            if (fuserType.equals(SpcyConstant.HCY_USERTYPE_JCY)) {
                roleID = SpcyConstant.getRoleIdByCode(ywflCode);
            }else {
                roleID = SpcyConstant.getZjRoleIdByCode(ywflCode);
            }

            YcSysUserRole findUserRole = new YcSysUserRole();
            findUserRole.setUserId(oldUserId);
            findUserRole.setRoleId(roleID);
            // 查询该用户和该角色是否已经存在
            List<YcSysUserRole> YcSysUserRoleList = ycSysUserMapper.getYcUserRole(findUserRole);

            if (CollectionUtils.isEmpty(YcSysUserRoleList)) {
                // 不存在，新增角色关系
                YcSysUserRole ycSysUserRole = new YcSysUserRole();
                ycSysUserRole.setId(GetUuIdUtils.ReplaceUuId());
                ycSysUserRole.setUserId(oldUserId);
                ycSysUserRole.setRoleId(roleID);
                // 角色id 不为空
                if (StringUtils.isNotEmpty(roleID)) {
                    ycSysUserMapper.addYcSysUserRole(ycSysUserRole);

                    String ycAddUserRoleOperCont = "新增用户角色联系：用户名["+sysUser.getRealname()+"]; 用户编号["+oldUserId+"]; " +
                            "角色编号["+roleID+"];]";
                    spcyLogService.addLogs(LogConstant.LOGS_SYS_USER,ycAddUserRoleOperCont,oldUserId,SpcyConstant.LOGS_SYS_TYPE_XZ,SpcyConstant.LOGS_SYS_TYPE_BC);
                }
            }
        } // 结束用户新增 操作

        /**
         * 核查员 新增 操作
         */

        // 旧用户id 不为空
        if (StringUtils.isNotEmpty(oldUserId)){
            String userType = ""; // 检查员、专家
            if (rsHcy != null && StringUtils.isNotEmpty(rsHcy.getFusertype())){
                userType = rsHcy.getFusertype();
            }
            // 该业务分类下，核查员是否存在
            List<SpcyRsHcy> spcyRsHcies = spcyRsHcyMapper.selectList(new LambdaQueryWrapper<SpcyRsHcy>()
                    .eq(SpcyRsHcy::getFuserid, oldUserId)
                    .eq(SpcyRsHcy::getFywflcode, ywflCode)
                    .eq(StringUtils.isNotEmpty(userType),SpcyRsHcy::getFusertype, userType));
            if (CollectionUtils.isEmpty(spcyRsHcies)){
                // 检查员不存在，新增
                String hcyId = GetUuIdUtils.ReplaceUuId();
                rsHcy.setId(hcyId);
                rsHcy.setFuserid(oldUserId);
                rsHcy.setFywflcode(ywflCode);
                rsHcy.setCreateTime(new Date());

                if (isZjType){
                    if (isAddZj){
                        rsHcy.setFssk(SpcyConstant.HCY_SSK_ZZK);
                    }else {
                        rsHcy.setFssk(SpcyConstant.HCY_SSK_ZYK);
                    }
                }

                if (isJcyType){
                    if (StringUtils.isBlank(rsHcy.getFssk())) {
                        rsHcy.setFssk(SpcyConstant.HCY_SSK_ZYK);
                    }else if (rsHcy.getFssk().contains(SpcyConstant.HCY_SSK_ZY)){
                        rsHcy.setFssk(SpcyConstant.HCY_SSK_ZYK);
                    }else if (rsHcy.getFssk().contains(SpcyConstant.HCY_SSK_ZZ)){
                        rsHcy.setFssk(SpcyConstant.HCY_SSK_ZZK);
                    }else if (rsHcy.getFssk().contains(SpcyConstant.HCY_SSK_YBK)){
                        rsHcy.setFssk(SpcyConstant.HCY_SSK_YB);
                    }else {
                        rsHcy.setFssk(SpcyConstant.HCY_SSK_ZYK);
                    }
                }

                CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
                if (currentUser != null) {
                    rsHcy.setCreateBy(currentUser.getName());
                }
                spcyRsHcyMapper.insert(rsHcy);

                String addInspectorOperCont = "新增核查员: 检查员编号["+hcyId+"]; 用户编号["+oldUserId+"];";
                spcyLogService.addLogs(LogConstant.LOGS_SYS_USER,addInspectorOperCont,oldUserId,SpcyConstant.LOGS_SYS_TYPE_XZ,SpcyConstant.LOGS_SYS_TYPE_BC);
            }else {
                // 核查员不属于有效变成有效
                if (!spcyRsHcies.get(0).getFstate().equals(SpcyCommonConstant.JCY_STATE_YX)) {
                    // 存在
                    SpcyRsHcy inspector = new SpcyRsHcy();
                    inspector.setId(spcyRsHcies.get(0).getId());
                    inspector.setFstate(SpcyCommonConstant.JCY_STATE_YX); // 有效
                    spcyRsHcyMapper.updateById(inspector);
                }
                SpcyRsHcy updateHcy = spcyRsHcies.get(0);
                updateHcy.setFszly(rsHcy.getFszly());
                updateHcy.setFxllx(rsHcy.getFxllx());
                updateHcy.setFlx(rsHcy.getFlx());

                if (isZjType){
                    if (isAddZj){
                        updateHcy.setFssk(SpcyConstant.HCY_SSK_ZZK);
                    }else {
                        updateHcy.setFssk(SpcyConstant.HCY_SSK_ZYK);
                    }
                }

                if (isJcyType && StringUtils.isBlank(updateHcy.getFssk())){
                    if (StringUtils.isBlank(rsHcy.getFssk())) {
                        updateHcy.setFssk(SpcyConstant.HCY_SSK_ZYK);
                    }else if (rsHcy.getFssk().contains(SpcyConstant.HCY_SSK_ZY)){
                        updateHcy.setFssk(SpcyConstant.HCY_SSK_ZYK);
                    }else if (rsHcy.getFssk().contains(SpcyConstant.HCY_SSK_ZZ)){
                        updateHcy.setFssk(SpcyConstant.HCY_SSK_ZZK);
                    }else if (rsHcy.getFssk().contains(SpcyConstant.HCY_SSK_YBK)){
                        updateHcy.setFssk(SpcyConstant.HCY_SSK_YB);
                    }else {
                        updateHcy.setFssk(SpcyConstant.HCY_SSK_ZYK);
                    }
                }

                updateHcy.setFspsj(rsHcy.getFspsj());
                updateHcy.setFspjssj(rsHcy.getFspjssj());
                spcyRsHcyMapper.updateById(updateHcy);
            }
        }

    }

    @Override
    public List<RsHcyVo> getRsHcyList(RsHcyVo rsHcyVo) {
        return spcyRsHcyMapper.getRsHcyList(rsHcyVo);
    }

    @Override
    public IPage<RsHcyVo> pageRsHcyList(Page<RsHcyVo> page, RsHcyVo rsHcyVo,String orderItem,String orderType) {
        return spcyRsHcyMapper.pageRsHcyList(page, rsHcyVo, orderItem, orderType);
    }

    @Override
    public List<HcyJbxxVo> findRsHcyList(RsHcyVo rsHcyVo) {
        return spcyRsHcyMapper.findRsHcyList(rsHcyVo);
    }

    @Override
    public boolean verifyLbJcyIsExist(String fjcylb, String userid) {
        List<SpcyRsHcy> jcyList = spcyRsHcyMapper.selectList(new LambdaQueryWrapper<SpcyRsHcy>()
                .eq(SpcyRsHcy::getFusertype, SpcyConstant.HCY_USERTYPE_JCY)
                .eq(SpcyRsHcy::getFuserid, userid)
                .eq(SpcyRsHcy::getFlx, fjcylb)
                .isNotNull(SpcyRsHcy::getFlx));

        if (CollectionUtil.isNotEmpty(jcyList)){
            return true;
        }

        return false;
    }

    @Override
    public boolean verifyLbJcyIsExist(String fjcylb, String fjcyxl, String userid) {
        List<SpcyRsHcy> jcyList = spcyRsHcyMapper.selectList(new LambdaQueryWrapper<SpcyRsHcy>()
                .eq(SpcyRsHcy::getFusertype, SpcyConstant.HCY_USERTYPE_JCY)
                .eq(SpcyRsHcy::getFuserid, userid)
                .eq(SpcyRsHcy::getFlx, fjcylb)
                .eq(SpcyRsHcy::getFjcyxl, fjcyxl)
                .isNotNull(SpcyRsHcy::getFlx));

        if (CollectionUtil.isNotEmpty(jcyList)){
            return true;
        }

        return false;
    }

    @Override
    public JcyExistLbBo verifyLbJcyIsExistList(String fjcylb, String fjcyxl, String userid) {
        JcyExistLbBo existLbBo = new JcyExistLbBo();
        List<SpcyRsHcy> jcyList = spcyRsHcyMapper.selectList(new LambdaQueryWrapper<SpcyRsHcy>()
                .eq(SpcyRsHcy::getFusertype, SpcyConstant.HCY_USERTYPE_JCY)
                .eq(SpcyRsHcy::getFuserid, userid)
                .eq(SpcyRsHcy::getFlx, fjcylb)
                .eq(SpcyRsHcy::getFjcyxl, fjcyxl)
                .isNotNull(SpcyRsHcy::getFlx));

        if (CollectionUtil.isNotEmpty(jcyList)){
            existLbBo.setSfcz("是");
            existLbBo.setFjcyid(jcyList.get(0).getId());
            existLbBo.setFuserid(jcyList.get(0).getFuserid());
            existLbBo.setJcy(jcyList.get(0));
        }else {
            existLbBo.setSfcz("否");
        }

        return existLbBo;
    }

    @Override
    public void updateJcyStateSsk(String userId, String lb, String ssk) {
        LambdaUpdateWrapper<SpcyRsHcy> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SpcyRsHcy::getFuserid, userId);
        updateWrapper.eq(SpcyRsHcy::getFusertype, SpcyConstant.HCY_USERTYPE_JCY);
        updateWrapper.eq(SpcyRsHcy::getFlx, lb);
        updateWrapper.isNotNull(SpcyRsHcy::getFlx);

        SpcyRsHcy jcy = new SpcyRsHcy();
        jcy.setFstate(SpcyCommonConstant.JCY_STATE_YX);
        jcy.setFssk(ssk);
        spcyRsHcyMapper.update(jcy, updateWrapper);
    }

    @Override
    public void updateJcyStateSsk(String userId, String fjcylb, String fjcyxl, String ssk) {
        LambdaUpdateWrapper<SpcyRsHcy> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SpcyRsHcy::getFuserid, userId);
        updateWrapper.eq(SpcyRsHcy::getFusertype, SpcyConstant.HCY_USERTYPE_JCY);
        updateWrapper.eq(SpcyRsHcy::getFlx, fjcylb);
        updateWrapper.eq(SpcyRsHcy::getFjcyxl, fjcyxl);
        updateWrapper.isNotNull(SpcyRsHcy::getFlx);

        SpcyRsHcy jcy = new SpcyRsHcy();
        jcy.setFstate(SpcyCommonConstant.JCY_STATE_YX);
        jcy.setFssk(ssk);
        spcyRsHcyMapper.update(jcy, updateWrapper);
    }

    @Override
    public void addNewLbJcy(SpcyRsHcy jcy) {
        if (StringUtils.isBlank(jcy.getId())) {
            jcy.setId(GetUuIdUtils.ReplaceUuId());
        }
        jcy.setCreateTime(new Date());
        jcy.setUpdateTime(new Date());
        jcy.setCreateBy(GetCurrentUserUtils.getCurrentUserAccount());
        jcy.setUpdateBy(GetCurrentUserUtils.getCurrentUserAccount());
        spcyRsHcyMapper.insert(jcy);

        // 新增日志
        StringBuffer addSysUserOper = new StringBuffer("新增检查员类别：");
        if (StringUtils.isNotBlank(jcy.getFlx())) {
            addSysUserOper.append("将[检查员类型】设置为").append(jcy.getFlx()).append("]；");
        }
        if (StringUtils.isNotBlank(jcy.getFssk())) {
            addSysUserOper.append("将[组长/组员/预备检查员】设置为[").append(jcy.getFssk()).append("]；");
        }
        if (StringUtils.isNotBlank(jcy.getFxllx())){
            addSysUserOper.append("将[序列类型】设置为").append(jcy.getFxllx()).append("]；");
        }
        if (StringUtils.isNotBlank(jcy.getFjcyxl())){
            addSysUserOper.append("将[检查员小类】设置为").append(jcy.getFjcyxl()).append("]；");
        }

        spcyLogService.addLogs(LogConstant.LOGS_JCYGL,addSysUserOper.toString(),jcy.getFuserid(),"新增检查员身份类别",SpcyConstant.LOGS_SYS_TYPE_XZ);

    }

    @Override
    public void initNewLbJcy(String userid, String fxllx, String fjcylb, String fjcyxl, String ssk, String zytc) {
        SpcyRsHcy newJcy = new SpcyRsHcy();
        newJcy.setFuserid(userid);
        newJcy.setFlx(fjcylb);
        newJcy.setFxllx(fxllx);
        newJcy.setFjcyxl(fjcyxl);
        newJcy.setFusertype(SpcyConstant.HCY_USERTYPE_JCY);

        // 所属库
        newJcy.setFssk(ssk);
        newJcy.setFszly(zytc);

        addNewLbJcy(newJcy);
    }

    @Override
    public void initNewLbJcy(String newJcyid, String userid, String fxllx, String fjcylb, String fjcyxl, String ssk, String zytc) {
        SpcyRsHcy newJcy = new SpcyRsHcy();
        newJcy.setId(newJcyid);
        newJcy.setFuserid(userid);
        newJcy.setFlx(fjcylb);
        newJcy.setFxllx(fxllx);
        newJcy.setFjcyxl(fjcyxl);
        newJcy.setFusertype(SpcyConstant.HCY_USERTYPE_JCY);

        // 所属库
        newJcy.setFssk(ssk);
        newJcy.setFszly(zytc);

        addNewLbJcy(newJcy);
    }

    @Override
    public void initNewLxLbJcy(String newJcyid, String userid, String fxllx, String fjcylb, String fjcyxl, String ssk, String zytc, String lxwjh) {
        SpcyRsHcy newJcy = new SpcyRsHcy();
        newJcy.setId(newJcyid);
        newJcy.setFuserid(userid);
        newJcy.setFlx(fjcylb);
        newJcy.setFxllx(fxllx);
        newJcy.setFjcyxl(fjcyxl);
        newJcy.setFusertype(SpcyConstant.HCY_USERTYPE_JCY);

        // 所属库
        newJcy.setFssk(ssk);
        newJcy.setFszly(zytc);

        newJcy.setFlxwjh(lxwjh);
        newJcy.setFlxsj(DateConversionUtils.DateToStringYYYYMMDD(new Date()));

        addNewLbJcy(newJcy);
    }

    @Override
    public void initNewPrLbJcy(String newJcyid, String userid, String fxllx, String fjcylb, String fjcyxl, String ssk, String zytc, String prwjh) {
        SpcyRsHcy newJcy = new SpcyRsHcy();
        newJcy.setId(newJcyid);
        newJcy.setFuserid(userid);
        newJcy.setFlx(fjcylb);
        newJcy.setFxllx(fxllx);
        newJcy.setFjcyxl(fjcyxl);
        newJcy.setFusertype(SpcyConstant.HCY_USERTYPE_JCY);

        // 所属库
        newJcy.setFssk(ssk);
        newJcy.setFszly(zytc);

        newJcy.setFpywjh(prwjh);
        newJcy.setFspsj(DateConversionUtils.DateToStringYYYYMMDD(new Date()));

        addNewLbJcy(newJcy);
    }

    @Override
    public List<SpcyRsHcy> selectJcyListByUserId(String userid) {
        return spcyRsHcyMapper.selectList(new LambdaQueryWrapper<SpcyRsHcy>()
                .eq(SpcyRsHcy::getFuserid, userid)
                .isNotNull(SpcyRsHcy::getFlx)
                .eq(SpcyRsHcy::getFusertype, SpcyConstant.HCY_USERTYPE_JCY));
    }

    @Override
    public List<SpcyRsHcy> selectJcyYxList(String userid) {
        return spcyRsHcyMapper.selectList(new LambdaQueryWrapper<SpcyRsHcy>()
                .eq(SpcyRsHcy::getFuserid, userid)
                .eq(SpcyRsHcy::getFusertype, SpcyConstant.HCY_USERTYPE_JCY)
                .eq(SpcyRsHcy::getFstate, SpcyCommonConstant.JCY_STATE_YX));
    }

    @Override
    public List<SpcyRsHcy> selectJcyYxListByFlx(String userid, String flx) {
        return spcyRsHcyMapper.selectList(new LambdaQueryWrapper<SpcyRsHcy>()
                .eq(SpcyRsHcy::getFuserid, userid)
                .eq(SpcyRsHcy::getFlx, flx)
                .eq(SpcyRsHcy::getFstate, SpcyCommonConstant.JCY_STATE_YX)
                .last("order by instr ('组长,组员', fssk)"));
    }

    @Override
    public List<SpcyRsHcy> selectJcyYxListByFjcyxl(String userid, String fjcyxl) {
        return spcyRsHcyMapper.selectList(new LambdaQueryWrapper<SpcyRsHcy>()
                .eq(SpcyRsHcy::getFuserid, userid)
                .eq(SpcyRsHcy::getFusertype, SpcyConstant.HCY_USERTYPE_JCY)
                .eq(SpcyRsHcy::getFjcyxl, fjcyxl)
                .eq(SpcyRsHcy::getFstate, SpcyCommonConstant.JCY_STATE_YX));
    }

    @Override
    public List<SpcyRsHcy> selectJcyWxList(String userid) {
        return spcyRsHcyMapper.selectList(new LambdaQueryWrapper<SpcyRsHcy>()
                .eq(SpcyRsHcy::getFuserid, userid)
                .eq(SpcyRsHcy::getFusertype, SpcyConstant.HCY_USERTYPE_JCY)
                .eq(SpcyRsHcy::getFstate, SpcyCommonConstant.JCY_STATE_ZX));
    }

    @Override
    public List<SpcyRsHcy> selectJcyAllList(String userid) {
        return spcyRsHcyMapper.selectList(new LambdaQueryWrapper<SpcyRsHcy>()
                .eq(SpcyRsHcy::getFuserid, userid)
                .eq(SpcyRsHcy::getFusertype, SpcyConstant.HCY_USERTYPE_JCY));
    }

    @Override
    public List<YcSysUser> selectJcyUserList(YcSysUser user) {
        return spcyRsHcyMapper.selectJcyUserList(user);
    }

    @Override
    public List<YcSysUser> selectJcyUserJbxxList(YcSysUser user) {
        return spcyRsHcyMapper.selectJcyUserJbxxList(user);
    }

    @Override
    public List<SpcyRsHcy> selectJcyJbxxList(SpcyRsHcy bean) {
        return spcyRsHcyMapper.selectJcyJbxxList(bean);
    }

    @Override
    public void updateJcyLbYxState(String jcyid) {
        if (StringUtils.isBlank(jcyid)){
            return;
        }

        LambdaUpdateWrapper<SpcyRsHcy> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SpcyRsHcy::getId, jcyid);

        SpcyRsHcy updateJcy = new SpcyRsHcy();
        updateJcy.setFstate(SpcyCommonConstant.JCY_STATE_YX);
        spcyRsHcyMapper.update(updateJcy, updateWrapper);
    }

    @Override
    public void updateJcyLbWxState(String jcyid) {
        if (StringUtils.isBlank(jcyid)){
            return;
        }

        LambdaUpdateWrapper<SpcyRsHcy> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SpcyRsHcy::getId, jcyid);

        SpcyRsHcy updateJcy = new SpcyRsHcy();
        updateJcy.setFstate(SpcyCommonConstant.JCY_STATE_ZX);
        spcyRsHcyMapper.update(updateJcy, updateWrapper);
    }
}
